package com.service.admin.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class RedisMatchEngineCacheUtils {

	@Autowired
	@Qualifier("redisMatchEngineTemplate")
	private RedisTemplate<String,String> redisMatchEngineTemplate;

	/**
	 * 新增不过期的数据
	 */
	public String setNoExpire(String key, String value) {
		redisMatchEngineTemplate.opsForValue().set(key, value);
		return "OK";
	}

	/**
	 * 设置带过期时间的数据
	 */
	public String set(String key, String value, int expireSeconds) {
		redisMatchEngineTemplate.opsForValue().set(key, value, expireSeconds, TimeUnit.SECONDS);
		return "OK";
	}

	/**
	 * 设置Mmap缓存数据
	 */
	public void set(String key, Map<String, String> values) {
		redisMatchEngineTemplate.opsForHash().putAll(key, values);
	}

	/**
	 * 设置Mmap缓存数据
	 */
	public void setBigDecimalMap(String key, Map<BigDecimal, BigDecimal> values) {
		redisMatchEngineTemplate.opsForHash().putAll(key, values);
	}

	/**
	 * 读取
	 */
	public String get(String key) {
		return redisMatchEngineTemplate.opsForValue().get(key);
	}

	/**
	 * 删除
	 */
	public void delete(String... keys) {
		redisMatchEngineTemplate.delete(Arrays.asList(keys));
	}

	/**
	 * 设置到期时间
	 */
	public void expire(String key, int seconds) {
		redisMatchEngineTemplate.expire(key, seconds, TimeUnit.SECONDS);
	}

	/**
	 * 获取(删除用)
	 */
	public Set<String> keys(String keys) {
		return redisMatchEngineTemplate.keys(keys);
	}


}
